﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using LitJson;
public class PlayGameMenuLogic : SQMenuBase
{
    private string Path = "Client/UI640/";
    public GameObject _playGameMenu;
    private Transform talkBg;
    private TalkData _talkListData;
    private float talkItemInitPosX=0f;
    private float talkItemInitPosY=148f;
    private float lineSpace=38f;
    private bool current = true;
    public override void onStart()
    {
        base.onStart();
        talkBg = _playGameMenu.transform.Find("chatBg");
        activityData();
        initTalkView();
    }
    public void activityData()
    {
        _talkListData = JsonMapper.ToObject<TalkData>(talkTestStr);

    }
    // 初始化显示
    public void initTalkView()
    {
        Object talkItem = Resources.Load(Path + "btnTalk");
        int itemNumber = _talkListData.talk.Count;
        for (int j = 0; j < itemNumber; j++)
        {
            GameObject _talkItem = (GameObject)Instantiate(talkItem);
            _talkItem.name = "btnTalk" + j;
            SparkExtensions.setParent(_talkItem, talkBg.gameObject, talkItemInitPosX, (talkItemInitPosY - lineSpace * j));
            foreach (System.Reflection.PropertyInfo p in _talkListData.talk[j].GetType().GetProperties())
            {
                string childName = "txt_" + p.Name;
                string showTxt = p.GetValue(_talkListData.talk[j], null).ToString();
                SparkExtensions.SetChildText(_talkItem, childName, showTxt);
            }
            UIEventListener.Get(_talkItem).onClick = initTalkText;

        }
       
    }
    public override void OnClick(GameObject btn)
    {
        switch (btn.name)
        {
            case "btnExit":
                UIManager.Instance.unpopMenu();
                SQManager.Instance.soundManager.playPreBGM();
                break;
            case "btnChat":
                ChatBG();
                break;
            case "btnAuto":
                break;
            default:
                break;
        }
    }
    //聊天背景框的显示
    public void ChatBG()
    {
        if (current)
        {
            talkBg.gameObject.SetActive(true);
            current = false;
        }
        else
        {
            talkBg.gameObject.SetActive(false);
            current = true;
        }
    }
    //初始化发送信息的内容
    public void initTalkText(GameObject btn)
    {
        ChatBG();
        Transform talkBg = _playGameMenu.transform.Find("talkBg");
        talkBg.gameObject.SetActive(true);
        GameObject childObj = btn.FindChild("txt_talkDes");
        SparkExtensions.SetChildText(_playGameMenu, "talkBg/txt_Text", childObj.GetComponent<UILabel>().text);

    }

    private string talkTestStr = @"
{
    ""talk"": [
        {
            ""talkDes"": ""非vip用户，或者vip还有1天到期的用户""
        },
        {
            ""talkDes"": ""非vip用户，或者vip还有2天到期的用户""
        },
        {
            ""talkDes"": ""非vip用户，或者vip还有3天到期的用户""
        },
        {
            ""talkDes"": ""非vip用户，或者vip还有4天到期的用户""
        },
        {
            ""talkDes"": ""非vip用户，或者vip还有5天到期的用户""
        },
        {
            ""talkDes"": ""非vip用户，或者vip还有6天到期的用户""
        },
        {
            ""talkDes"": ""非vip用户，或者vip还有7天到期的用户""
        },
        {
            ""talkDes"": ""非vip用户，或者vip还有8天到期的用户""
        }
    ]
}";

}